<?php

class Painel_Form_Stagenda extends Zend_Form
{

    public function init()
    {
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        
        $this->setName('form_edt');
        
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        
        $auth = Zend_Auth::getInstance();
        $s_user = $auth->getStorage();
        $_PER_ID = $s_user->read()->PER_ID;
        $_USU_ID = $s_user->read()->USU_ID;
        
        $id = $front->getRequest()->getParam('id', 0);
        
        $el = new Zend_Form_Element_Hidden('AGD_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('AGD_DAT');
        $el->setLabel('Data');
        $el->setRequired(true);
        $el->setAttrib('class', 'form-control data required');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('AGD_HOR');
        $el->setLabel('Hora');
        $el->setRequired(true);
        $el->setAttrib('class', 'form-control hora required');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Textarea('AGD_OBS');
        $el->setLabel('Observação');
        $el->setAttrib('rows', '5');
        $el->setAttrib('class', 'form-control');
        $this->addElement($el);
        
        $query = 'SELECT SQL_CACHE USU_ID,USU_NOM FROM USUARIOS WHERE USU_STA = 1 AND TUS_ID = 2 ORDER BY USU_NOM ASC';
        $result = $db->fetchAll($query);
        $arr = array('' => 'Nenhum');
        foreach($result as $r) $arr[$r['USU_ID']] = $r['USU_NOM'];
        $el = new Zend_Form_Element_Select('USU_IDFUN',array('onchange' => ''));
        $el->setLabel('Funcionário')
        ->addMultiOptions($arr);
        $el->setRegisterInArrayValidator(false);
        $el->setAttrib('class', 'form-control');
        if($id > 0 && $_PER_ID > 5) $el->setAttrib('disabled', '');
        $this->addElement($el);
        
        $query = 'SELECT SQL_CACHE b.*,c.CTS_NOM FROM ST_SERVICO b, ST_CATEGORIA_SERVICO c WHERE b.CTS_ID = c.CTS_ID ORDER BY SER_NOM ASC';
        $result = $db->fetchAll($query);
        $arr = array(''=>'Nenhum');
        foreach($result as $r) $arr[$r['SER_ID']] = $r['CTS_NOM'].' '.$r['SER_NOM'];
        $el = new Zend_Form_Element_Select('SER_ID',array('onchange' => ''));
        $el->setLabel('Serviço')
        ->addMultiOptions($arr);
        $el->setRegisterInArrayValidator(false);
        $el->setAttrib('class', 'form-control required');
        $this->addElement($el);
        
        
        $query = 'SELECT SQL_CACHE CLI_ID,CLI_NOM FROM CLIENTES WHERE CLI_STA = 1 ORDER BY CLI_NOM ASC';
        $result = $db->fetchAll($query);
        $arr = array('' => 'Nenhum');
        foreach($result as $r) $arr[$r['CLI_ID']] = $r['CLI_NOM'];
        $el = new Zend_Form_Element_Select('CLI_ID',array('onchange' => ''));
        $el->setLabel('Cliente cadastrado')
        ->addMultiOptions($arr);
        $el->setRegisterInArrayValidator(false);
        $el->setAttrib('class', 'form-control');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('AGD_CLINOM');
        $el->setLabel('Cliente');
        $el->setAttrib('class', 'form-control');
        if($id > 0) $el->setAttrib('readonly', '');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('AGD_CLITEL');
        $el->setLabel('Telefone');
        $el->setAttrib('class', 'form-control telefone');
        if($id > 0) $el->setAttrib('readonly', '');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Textarea('AGD_MSG');
        $el->setLabel('Mensagem');
        $el->setAttrib('rows', '5');
        $el->setAttrib('class', 'form-control');
        if($id > 0) $el->setAttrib('readonly', '');
        $this->addElement($el);
        
        if($_PER_ID <= 5) { 
            $el = new Zend_Form_Element_Checkbox('AGD_STA',array('checked'=>'checked'));
            $el->setLabel('Ativo');
            $this->addElement($el);
        }
        
        $el = new Zend_Form_Element('br',array( 'helper'=>'formNote', 'value'=>'&nbsp;' ));
        $this->addElement($el);
        
        $btn = new Zend_Form_Element_Button('Salvar',array('disableLoadDefaultDecorators' =>true));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<button type="submit" class="btn btn-lg btn-success"><span class="glyphicon glyphicon-floppy-saved" aria-hidden="true"></span> Salvar</button>');
        $this->addElement($btn);
        
        $btn = new Zend_Form_Element_Button('Cancelar',array('disableLoadDefaultDecorators' =>true));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="'.$url.'" class="btn btn-lg btn-danger"><span class="glyphicon glyphicon-floppy-remove" aria-hidden="true"></span> Cancelar</a>');
        $this->addElement($btn);
     
    }


}

